(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("jobExecutionService", jobExecutionService);
	jobExecutionService.$inject = [ '$q', 'loggerFactory', 'ajaxServiceFactory', 'modalService' ];
	function jobExecutionService($q, loggerFactory, ajaxServiceFactory, modalService) {
		var log = loggerFactory.getLogger("jobgrpSchdService");
		
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getYesno : {
				url : 'do/cda/dataList/yesno',
				method : 'post'
			},
			testSchd : {
				url : 'do/cda/jobGroup/testSchd',
				method : 'post'
			}
		});
		
		function getYesnoOptions() {
			var defer = $q.defer();
			ajaxService.getYesno().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		
		function doTestSchd(jobgrpSchd) {
			var defer = $q.defer();
			ajaxService.testSchd(jobgrpSchd).then(function(response) {
				defer.resolve(response);
			},
			function(response){
				modalService.openErrorDialogs({
					'messages' : response.error
				});
			});
			return defer.promise;
		}
		
		function getTypeOptions() {
			var option = {
					"data" : [ {
						"value" : "",
						"label" : ""
					}, {
						"value" : "Date",
						"label" : "Date"
					}, {
						"value" : "SQL Date",
						"label" : "SQL Date"
					}, {
						"value" : "Timestamp",
						"label" : "Timestamp"
					} , {
						"value" : "Time",
						"label" : "Time"
					} , {
						"value" : "String",
						"label" : "String"
					} , {
						"value" : "Boolean",
						"label" : "Boolean"
					} , {
						"value" : "Double",
						"label" : "Double"
					} , {
						"value" : "Float",
						"label" : "Float"
					} , {
						"value" : "Long",
						"label" : "Long"
					} , {
						"value" : "Integer",
						"label" : "Integer"
					} , {
						"value" : "Short",
						"label" : "Short"
					} , {
						"value" : "Byte",
						"label" : "Byte"
					} ]
			};
			return option;
		}
		
		function getFormatOptions() {
			var option = {
					"data" : [ {
						"value" : "",
						"label" : ""
					}, {
						"value" : "date",
						"label" : "date"
					}, {
						"value" : "timestamp",
						"label" : "timestamp"
					}, {
						"value" : "time",
						"label" : "time"
					}  ]
			};
			return option;
		}
		
		function initDataOptions(ctrl) {
			
			getYesnoOptions().then(function(response) {
				ctrl.customFlagOptions = response;
				ctrl.customFlagOptions.hasEmptyOption = true;
			});
			ctrl.typeOptions = getTypeOptions();
			
			ctrl.schdTypeOptions = getSchdTypeOptions();
			
			ctrl.formatOptions = getFormatOptions();
			
		}
		function getSchdTypeOptions() {
			var option = {
					"data" : [ {
						"value" : "N",
						"label" : "NORMAL"
					}, {
						"value" : "D",
						"label" : "DAILY"
					}, {
						"value" : "M",
						"label" : "MONTHLY"
					}, {
						"value" : "Y",
						"label" : "YEARLY"
					} ]
			};
			return option;
		}
		
		function testSchd(jobgrpSchd){
			doTestSchd(jobgrpSchd).then(function(response) {
				if(response){
					modalService.openCustomDialogs({
						'dialogsModule' : 'cda.jobGroup.testSchd',
						'title' : 'title.dialogs.jobschd',
						'params' : {
							"runTimeList" : response.data
						}
					});
				}
			});
		}
		
		function checkParameter(params) {
			var hash = {};
			for ( var i in params) {
				if (hash[params[i].name])
					return true;
				hash[params[i].name] = true;
			}
			return false;
		}
		
		return {
			initDataOptions : initDataOptions,
			testSchd : testSchd,
			checkParameter : checkParameter
		};
	}
})(angular);